package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.StoreDao;
import cn.hs.ph.entity.Store;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StoreDaoImpl implements StoreDao {
    @Override
    public int insert(Store store) {
        String sql = "insert into shop values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,null)";
        Object[] obj ={
                store.getShopName(),
                store.getShopDescribe(),
                store.getShopImage(),
                store.getShopPhone(),
                store.getShopProvince(),
                store.getShopCity(),
                store.getShopDistrict(),
                store.getShopAddress(),
                store.getShopLongitude(),
                store.getShopLatitude(),
                store.getShopStatus(),
                store.getAddTime(),
                store.getShopUserId(),
                store.getShopAccount(),
                store.getShopPassword(),
                store.getShopSort(),
                store.getSalesVolume(),
                store.getDistance(),
                store.getShopCommunitySort(),
                store.getConcernStatus(),
                store.getShopOrderCount(),
                store.getShopCategoryList(),
                store.getShopGoodsList()
        };
        return JDBCDruidUtil.update(sql, obj);

    }

    @Override
    public int delete(int shopId) {
       String sql = "delete from shop where shopId = ?";
       Object[] obj = {shopId};
       return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public int update(Store store) {
        String sql = "update shop set shopName=?,shopImage=?,shopPhone=?,shopProvince=?,shopCity=?,shopDistrict=?,shopAddress=?,shopLongitude=?,shopLatitude=?,shopDescribe=? where shopId = ?";
        Object[] obj={
                store.getShopName(),
                store.getShopImage(),
                store.getShopPhone(),
                store.getShopProvince(),
                store.getShopCity(),
                store.getShopDistrict(),
                store.getShopAddress(),
                store.getShopLongitude(),
                store.getShopLatitude(),
                store.getShopDescribe(),
                store.getShopId()
        };
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public List<Store> selectAll(Map<String, String> map) {
        List<Store> list = new ArrayList<>();
        String sql = "select * from shop where 1=1 ";
        List<Object> params = new ArrayList<>();
        String keyword = map.get("keyword");
        if (keyword != null && !keyword.isEmpty()) {
            sql += " and shopName like ? ";
            params.add("%"+keyword+"%");
        }
        String phone = map.get("phone");
        if (phone != null && !phone.isEmpty()) {
            sql += " and shopPhone like ? ";
            params.add("%"+phone+"%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !startTime.isEmpty()) {
            sql += " and addTime between ? and ?  ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?,?  ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;

                Store store = new Store(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getDate(13),
                        res.getInt(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getInt(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getInt(22),
                        res.getString(23),
                        res.getString(24)
                );

                list.add(store);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();

        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        String sql = "select count(0) from shop where 1=1 ";
        List params = new ArrayList();
        String keyword = map.get("keyword");
        if (keyword != null && !"".equals(keyword)) {
            sql += " and shopName like ? ";
            params.add("%" + keyword + "%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?  ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                count = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();

        return count;

    }

    @Override
    public List<Store> lookStore(Map<String, String> map) {
        List<Store> list = new ArrayList<>();
        List<Object> params = new ArrayList();

        String sql = "select * from shop where 1=1 ";

        String communityId = map.get("communityId");
        if (communityId!=null&&!"".equals(communityId)){
            sql+=" and communityId = ? ";
            params.add(communityId);
        }else {
            sql+=" and communityId is null ";
        }


        String shopName = map.get("shopName");
        if (shopName!=null&&!"".equals(shopName)){
            sql+=" and shopName like ? ";
            params.add("%"+shopName+"%");
        }

        String pn = map.get("pageNum");
        if (pn!=null&&!"".equals(pn)){
            sql+=" limit ?,?";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()){
                    break;
                }
                Store store = new Store(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getDate(13),
                        res.getInt(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getInt(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getInt(22),
                        res.getString(23),
                        res.getString(24)
                );
                list.add(store);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int lookStoreCount(Map<String, String> map) {

        int count = 0;
        List<Object> params = new ArrayList();

        String sql = "select count(1) from shop where communityId = ?";
        String communityId = map.get("communityId");
        params.add(communityId);

        String shopName = map.get("shopName");
        if (shopName!=null&&!"".equals(shopName)){
            sql+=" and shopName like ? ";
            params.add("%"+shopName+"%");
        }

        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()){
                    break;
                }
                count = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return count;
    }

    @Override
    public int addStore(int shopId[],int communityId) {
        int count = 0;
        String sql = "update shop set communityId = ? where shopId = ? ";
        for (int i = 0;i<shopId.length;i++){
            Object obj[]={
                    communityId,
                    shopId[i]
            };
            count += JDBCDruidUtil.update(sql,obj);
        }
        return count;
    }


}
